<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入姓名"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          style="width: 200px"
        >
          <el-option label="待受理" :value="0" />
          <el-option label="已受理" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="complainList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="80" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column
        label="电话"
        align="center"
        prop="phone"
        min-width="100"
      />
      <el-table-column
        label="小区名称"
        align="center"
        prop="communityId"
        min-width="100"
      >
        <template #default="scope">
          <span>{{ matchCommunity(scope.row.communityId).name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="房屋编号" align="center" prop="roomNum" />
      <el-table-column
        label="内容"
        align="center"
        prop="content"
        min-width="200"
      />
      <el-table-column label="图片" align="center" prop="images">
        <template #default="scope">
          <ImagePreview
            v-for="image in scope.row.images.split(',') || []"
            :key="image"
            :src="image"
            width="300"
            height="300"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === 0 ? 'warning' : 'success'">
            {{ scope.row.status === 0 ? "待受理" : "已受理" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="提交时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="受理人"
        align="center"
        prop="auditBy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="受理时间"
        align="center"
        prop="auditTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.auditTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <AddEdit
      v-model="addEditVisible"
      v-if="addEditVisible"
      :row="curRow"
      @submit="getList"
    />
  </div>
</template>

<script setup name="Complain">
import { getComplainList, deleteComplain } from "@/api/system/complain";
import { matchCommunity } from "@/common/match";
import ImagePreview from "@/components/ImagePreview";
import { ElMessageBox, ElMessage } from "element-plus";
import AddEdit from "./addEdit.vue";
const { proxy } = getCurrentInstance();

const complainList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const addEditVisible = ref(false);
const curRow = ref({});
const data = reactive({
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    realName: "",
    status: "",
  },
});

const { queryParams } = toRefs(data);

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 查询投诉建议列表 */
function getList() {
  loading.value = true;
  getComplainList(queryParams.value).then(({ data }) => {
    complainList.value = data.records;
    total.value = data.total;
    loading.value = false;
  });
}

function handleUpdate(row) {
  curRow.value = row;
  addEditVisible.value = true;
}
function handleDelete(row) {
  ElMessageBox.confirm("确定要删除吗？").then(() => {
    deleteComplain(row.id).then(({ data }) => {
      if (data) {
        ElMessage.success("删除成功");
        getList();
      } else {
        ElMessage.error("删除失败");
      }
    });
  });
}

getList();
</script>
